<!--
* description: 采购销退出库
* author: ligy
* createdDate: 2021-3-4
-->

<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :key="oneTableKey"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-api-config="apiConfig"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="true"
      :dynamic-is-show-more-btn="true"
      :dynamic-is-init-table="true"
      :dynamic-api-style="'restful'"
      :callback-obj="callbackObj"
    />

    <edit
      :key="editPopupsKey"
      :dynamic-edit-row-data="editRowData"
      :popups-visible="editPopupsVisible"
      :popups-state="editPopupsState"
      :dynamic-api-style="'restful'"
      @close="close"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from "@/components/mixins/oneTableWithViewTemplateMixins";
import OneTableTemplate from "@/components/templates/oneTable";
import request from "@/utils/request";
import Vue from "vue";
import api from "@/api/pa/apiBranchWarehouse";
export default {
  name: "PurchaseSalesReturn",

  components: {
    OneTableTemplate,
    // Edit: () => import('./edit')
  },
  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins],

  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, ""], function () {
      next();
    });
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path;

    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode]);

    return {
      editRowData: {},
      selectiondata: [],
      // 网格查询API配置对象
      apiConfig: api.queryReturnOrderOutStorageInfo,

      // 动态组件-按钮
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          name: "search",
          clickEvent: () => this.queryTable(1),
          text: this.$t("sys.button.query"), // 查询
          position: "right",
          fuzzySearch: true,
        },
        {
          compKey: "comKey2",
          type: "",
          size: "small",
          name: "",
          position: "left",
          clickEvent: () => this.outStorage(),
          text: "出库",
          disabled: false,
        },
        {
          compKey: "btnKey3",
          type: "",
          size: "small",
          name: "reset",
          clickEvent: () => this.reset(),
          text: this.$t("sys.button.reset"),
          position: "right", // 重置
        },
      ],

      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
              .tableComponents
          : [
              {
                compKey: "compKey1",
                labelName: "退货类型：",
                codeField: "purReturnType",
                component: () => import("@/components/org/LookupValue"),
                oFields: "statusId,statusText",
                options: [
                  { statusId: "440", statusText: "采购单销退" },
                  { statusId: "550", statusText: "呆滞件销退" },
                ],
                type: "dropdownList",
                isMul: false,
                isMust: true,
              },

              {
                compKey: "compKey2",
                labelName: "销退单号：",
                codeField: "purReturnCode",
                component: () => import("@/components/org/commonInput"),
                type: "inputText",
                isMul: false,
                isMust: true,
              },
              {
                compKey: "compKey3",
                labelName: "备件编码：",
                codeField: "partNo",
                component: () => import("@/components/org/commonInput"),
                type: "inputText",
                isMul: false,
                isMust: true,
              },
              {
                compKey: "compKey3",
                labelName: "备件名称：",
                codeField: "partName",
                component: () => import("@/components/org/commonInput"),
                type: "inputText",
                isMul: false,
                isMust: true,
              },
              {
                compKey: "compKey4",
                labelName: "单据日期：",
                codeField: "beginDate,endDate",
                component: () =>
                  import("@/components/org/datePicker/andtDatePicker"),
                type: "andtDatePicker",
                isMul: false,
                isMust: false,
              },
            ],

      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
              {
                prop: "warehouseId",
                label: "仓库ID",
                hidden: true,
                alwaysHidden: true,
              },
              {
                prop: "purReturnId",
                label: "消退ID",
                hidden: true,
                alwaysHidden: true,
              },
              {
                prop: "returnDlrId",
                label: "归还ID",
                hidden: true,
                alwaysHidden: true,
              },
              {
                prop: "updateControlId",
                label: "并发ID",
                hidden: true,
                alwaysHidden: true,
              },
              {
                prop: "purReturnCode",
                label: "销退单号",
              },
              {
                prop: "purReturnTypeCn",
                label: "退货类型",
              },
              {
                prop: "returnDate",
                label: "申请日期",
                width: "150px",
              },
              {
                prop: "partNo",
                label: "备件编码",
              },
              {
                prop: "partName",
                label: "备件名称",
              },
              {
                prop: "remark",
                label: "出库原因",
              },
              {
                prop: "accountQty",
                label: "库存数量",
              },
              {
                prop: "returnQty",
                label: "出库数量",
              },
              {
                prop: "retrospectCode",
                label: "追溯件号+",
                isComponent: true,
                comps: [
                  {
                    compKey: "compKey5" + this.$utils.generateId(),
                    codeField: "retrospectCode",
                    component: () => import("@/components/org/commonInput"),
                    type: "inputText",
                  },
                ],
              },
              {
                prop: "returnPrice",
                label: "出库单价",
              },
              {
                prop: "returnAmount",
                label: "出库金额",
              },
              {
                prop: "warehouseName",
                label: "仓库",
                width: "150px",
              },
              {
                prop: "storageLocation",
                label: "仓位",
              },
            ],
      callbackObj: {
        // 手动勾选时
        onCheckSelect: (selection, row) => {
          this.selectiondata = selection;
        },
      },
      // 表单查询数据
      formField: this.$utils.getFormField(
        this,
        {
          purReturnType: "",
          purReturnCode: "",
          partNo: "",
          partName: "",
          bigAreaName4: "",
          endDate: "",
        },
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode]
      ),
    };
  },

  mounted() {
    // 列表查询
    this.queryTable(1);
  },

  methods: {
    outStorage() {
      if (this.selectiondata.length > 0) {
        request({
          url: api.outStorage.APIUrl,
          method: "post",
          data: this.selectiondata,
        }).then((res) => {
          this.$message.success(res.msg);
          this.queryTable(1);
        });
      } else {
        this.$message.warning("请选择出库信息");
      }
    },
    // 编辑获取数据
    edit(index) {
      this.editPopupsState = "edit";
      this.editPopupsVisible = true;
      this.editRowData = this.$utils.deepCopy(
        this.$refs["multipleTable"].list[index]
      );
    },

    // 导出
    export() {
      var date = new Date();
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      month = month < 10 ? "0" + month : month;
      day = day < 10 ? "0" + day : day;
      var mydate = year.toString() + month.toString() + day.toString();
      const returnObj = {
        // api配置
        apiConfig: {
          APIUrl: "/mp/e3sh/basics/ve/CarTypeService/exportCarType.do",
        },
        variables: this.formField,
      };
      returnObj.variables.dataType = "excel";
      const paramD = this.$getParams(returnObj);
      this.$requestAPI(paramD).then((res) => {
        this.$utils.downloadFile(res, `车系维护${mydate}.xlsx`);
      });
    },
  },
};
</script>
